package com.yunchao.bank.splash

import com.yunchao.bank.configure.domain.repository.ConfigureRepository
import com.yunchao.feature.base.presentation.viewmodel.BaseEffect
import com.yunchao.feature.base.presentation.viewmodel.BaseState
import com.yunchao.feature.base.presentation.viewmodel.BaseViewModel

internal class SplashViewModel(
    private val configureRepository: ConfigureRepository
) : BaseViewModel<SplashViewModel.UiState, Nothing, SplashViewModel.UiEffect>(
    UiState.Idle
) {



    fun getConfigure() = safeLaunch {
        configureRepository.getConfigure()
            .onSuccess {
                if (it.serverIp.isNotBlank()){
                    postSideEffect(UiEffect.NavigateToMain)
                }else{
                    postSideEffect(UiEffect.NavigateToConfig)
                }
            }
            .onFailure {
                postSideEffect(UiEffect.NavigateToConfig)
            }
    }


    internal sealed interface UiEffect : BaseEffect {
        data object NavigateToMain : UiEffect
        data object NavigateToConfig : UiEffect
    }

    internal sealed interface UiState : BaseState {
        data object Idle : UiState
    }
}